package net.oschina.app.ui;

import net.oschina.app.AppContext;
import net.oschina.app.R;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.bean.FriendsList;
import net.oschina.app.fragment.SelectFriendsFragment;
import net.oschina.app.ui.empty.EmptyLayout;
import net.oschina.app.util.UIHelper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;

import butterknife.InjectView;


public class SelectFriendsActivity extends BaseActivity {

    /**
     * Activity的{@link #setTitle(int) 标题}<br/>
     * Type: @StringRes int
     */
    public static final String EXTRA_KEY_TTILE =
            SelectFriendsActivity.class.getName() + ".extra.KEY_TTILE";

    @Nullable
    private Integer mUid;

    @InjectView(R.id.error_layout)
    EmptyLayout mErrorLayout;

    public static Intent buildIntent(Context context, @Nullable @StringRes Integer titleId) {
        final Intent intent = new Intent(context, SelectFriendsActivity.class);
        if (titleId != null) {
            intent.putExtra(EXTRA_KEY_TTILE, titleId);
        }
        return intent;
    }

    @LayoutRes
    @Override
    protected int getLayoutId() {
        return R.layout.activity_friends_select;
    }

    @Override
    protected boolean hasBackButton() {
        return true;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        // 处理Intent
        final Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_KEY_TTILE)) {
            setTitle(intent.getIntExtra(EXTRA_KEY_TTILE, -1));
        }
        // 初始化属性
        if (getUid() == null) {
            UIHelper.showLoginActivity(this);
        }
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.friends_fragment_container);
        final Integer uid = getUid();
        if (fragment == null && uid != null) {
            fragment = SelectFriendsFragment.newInstance(FriendsList.TYPE_FOLLOWER, uid);
            fragmentManager.beginTransaction()
                    .add(R.id.friends_fragment_container, fragment)
                    .commit();
        }
        // fragment maybe null here(mUid is null)
    }

    @Override
    public void onClick(View view) {

    }

    @Nullable
    private Integer getUid() {
        if (mUid != null) {
            return mUid;
        }
        final AppContext appContext = AppContext.getInstance();
        if (appContext.isLogin()) {
            return mUid = appContext.getLoginUid();
        } else {
            return null;
        }
    }

}
